package com.wl.index.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wl.entity.Note;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface NoteMapper extends BaseMapper<Note> {
    //显示帖子所有内容
    @Select("select * from note where title=#{title}")
    List<Note> selectByTitle(String title);

    //在帖子表中根据作者id查询作者信息
    @Select("select u.uid,u.uname,u.zhanghao,u.head,u.bio from note n join lvuser u on n.uid=u.uid where n.uid=#{uid}")
    Note selectUserByUid(Integer uid);

    //根据帖子id查询帖子及其作者信息
    @Select("SELECT n.nid, n.title, n.content, n.img1, n.img2, n.img3, " +
            "n.created_at as createdAt, n.updated_at as updatedAt, " +
            "n.view_count as viewCount, n.like_count as likeCount, " +
            "n.favorite_count as favoriteCount, n.comment_count as commentCount, " +
            "u.uid, u.uname, u.head,u.zhanghao, u.bio, u.gender " +
            "FROM note n JOIN lvuser u ON n.uid = u.uid " +
            "WHERE n.nid = #{nid}")
    Note selectNoteauthorById(Integer nid);

    //查询所有帖子
    @Select("select * from note where sta=0 and uid=#{uid} ORDER BY created_at DESC")
    List<Note> selectAllValidNotes(Integer uid);

    //添加帖子
    @Insert("INSERT INTO note (" +
            "uid, ntid, title, content, " +
            "img1, img2, img3, " +
            "created_at, updated_at, " +
            "sta, view_count, like_count, favorite_count, comment_count" +
            ") VALUES (" +
            "#{uid}, #{ntid}, #{title}, #{content}, " +
            "#{img1}, #{img2}, #{img3}, " +
            "#{createdAt}, #{updatedAt}, " +
            "#{sta}, #{viewCount}, #{likeCount}, #{favoriteCount}, #{commentCount}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "nid") // 自动回填主键
    int insertNote(Note note);
}
